package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.dao.PriceDao;
import yf.mall.entity.*;
import yf.mall.enums.*;
import yf.mall.service.*;
import yf.mall.vo.ActGoodsVo;
import yf.mall.vo.MallOrderVo;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * que
 * 店铺controller
 */
@RestController
@RequestMapping("/api/shop")
public class ApiMyShopController {

    @Autowired
    private MallGoodsService mallGoodsService;

    @Autowired
    private MallTypeService mallTypeService;


    @Autowired
    private PriceDao priceDao;
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopLogTemService shopLogTemService;

    /**
     * que
     * 展示商家所有产品
     *
     * @param userId
     * @return
     */
    @RequestMapping("/goodsList")
    public R goodsList(@LoginUserId Long userId, Integer page, Integer limit, String order, Long uid) {
        if (order == null) {
            order = "sort,id desc";
        }
        //uid店主id  存在则是进店
        Integer shelf = null;
        PageHelper.startPage(page, limit).setOrderBy(order);
        List<MallGoodsEntity> mallGoodsEntityList;
        //进店
        if (uid != null) {
            if (uid.equals(Const.ADMIN_ID)) {
                uid = null;
            }
            shelf = MallGoodsShelfEnum.ON.getIndex();
            mallGoodsEntityList = mallGoodsService.findByUserId(uid, shelf);
        } else { //自己的
            mallGoodsEntityList = mallGoodsService.findByUserId(userId, shelf);
        }
        mallGoodsEntityList = mallGoodsService.filterByLevel(userId, mallGoodsEntityList);
        ShopEntity shop = new ShopEntity();
        if (uid != null) {
            shop = shopService.findByUserId(uid);
        } else {
            shop.setAvatar("1603263922406WechatIMG110.png");
            shop.setName("社集优选自营店");
        }

        return R.ok(mallGoodsEntityList).putExtra("shop", shop);
    }

    @Autowired
    GoodsQualificationService goodsQualificationService;
    @Autowired
    private SaleTypeInfoService saleTypeInfoService;
    @Autowired
    private MallActService mallActService;

    /**
     * 查看产品
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {

        MallGoodsEntity mallGoods = mallGoodsService.getWithSku(id);
        SaleTypeInfoEntity saleTypeInfoEntity = saleTypeInfoService.findByGoodsId(mallGoods.getId());
        List typesResult = mallTypeService.findByGoodsId(id);
        //获取产品分类资质
        GoodsQualificationEntity goodsQualificationEntity = goodsQualificationService.findByGoodsId(mallGoods.getId());
        MallActEntity act = mallActService.findByGoodsIdAndstatus(id, MallActStatusEnum.OPEN.getIndex());
        return R.ok(mallGoods).putExtra("saleTypeInfoEntity", saleTypeInfoEntity).putExtra("types", typesResult).putExtra("goodsQualificationEntity", goodsQualificationEntity).putExtra("act", act);
    }

    /**
     * 修改产品
     */
    @RequestMapping("/update")
    public R update(@LoginUserId Long userId, @RequestBody ActGoodsVo actGoodsVo) {

        if (actGoodsVo.getGoods() == null) {
            throw new RRException("产品不存在");
        }
        if (actGoodsVo.getGoods().getUserId() == null || !userId.equals(actGoodsVo.getGoods().getUserId())) {
            throw new RRException("只能修改自己的产品");
        }
        if (actGoodsVo.getGoods().getCreateTime() != null && !"".equals(actGoodsVo.getGoods().getCreateTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date createTime = sdf.parse(actGoodsVo.getCreateTime());
                actGoodsVo.getGoods().setCreateTime(createTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        mallGoodsService.merge(actGoodsVo, userId);

        return R.ok(actGoodsVo.getGoods());
    }

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserService userService;

    /**
     * que
     * 下架产品
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @RequestMapping("/goodsDown")
    public R goodsDown(@LoginUserId Long userId, Long goodsId) {
        if (goodsId == null) {
            throw new RRException("请选择产品");
        }
        MallGoodsEntity mallGoodsEntity = mallGoodsService.get(goodsId);
        if (mallGoodsEntity == null) {
            throw new RRException("产品不存在");
        }
        if (mallGoodsEntity.getUserId() == null || !mallGoodsEntity.getUserId().equals(userId)) {
            throw new RRException("只能下架自己的产品");
        }
        mallGoodsEntity.setShelf(MallGoodsShelfEnum.DOWN.getIndex());
        mallGoodsService.update(mallGoodsEntity);
//        UserEntity userEntity = this.userService.get(userId);
//        this.noticeService.sendGoodsDown(userEntity.getMobile());
        return R.ok(mallGoodsEntity);
    }

    /**
     * que
     * 上传产品
     */
    @RequestMapping("/save")
    public R save(@LoginUserId Long userId, @RequestBody ActGoodsVo actGoodsVo) {
        actGoodsVo.getGoods().setUserId(userId);
        actGoodsVo.getGoods().setShelf(MallGoodsShelfEnum.AUDIT.getIndex());
        if (actGoodsVo.getGoods().getCreateTime() != null && !"".equals(actGoodsVo.getGoods().getCreateTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date createTime = sdf.parse(actGoodsVo.getCreateTime());
                actGoodsVo.getGoods().setCreateTime(createTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        mallGoodsService.merge(actGoodsVo, userId);

        return R.ok(actGoodsVo.getGoods());
    }

    @Autowired
    private MallOrderService mallOrderService;

    /**
     * Long
     * 店铺订单管理
     *
     * @param userId
     * @param orderStatus
     * @param payStatus
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("shopOrderList")
    public R shopOrderList(@LoginUserId Long userId, Integer orderStatus, Integer payStatus, int page, Integer limit) {
        List<MallOrderVo> orderVos = this.mallOrderService.findByFromUserId(userId, orderStatus, payStatus, page, limit);
        return R.ok(orderVos);
    }

    /**
     * que
     * H5商家上传物流单号
     *
     * @param userId
     * @param orderId    订单id
     * @param logNo      物流单号
     * @param logCompany 物流公司
     * @param logFee     物流费
     * @return
     */
    @RequestMapping("apiUpload")
    public R apiUpload(@LoginUserId Long userId, Long orderId, String logNo, String logCompany, BigDecimal logFee) {
        mallOrderService.apiUpload(userId, orderId, logNo, logCompany, logFee);
        return R.ok();
    }

    /**
     * que
     * 展示所有物流规则
     *
     * @param userId
     * @return
     */
    @RequestMapping("shopLogTem")
    public R shopLogTem(@LoginUserId Long userId) {
        ShopEntity shopEntity = shopService.findByUserId(userId);
        List<ShopLogTemEntity> list = null;
        if (shopEntity != null) {
            list = shopLogTemService.getList(shopEntity.getId());
        }
        return R.ok(list);
    }

    /**
     * 提走订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/mention")
    public R mentionOrder(Long orderId) {
        MallOrderEntity mallOrderEntity = mallOrderService.mentionOrder(orderId);
        return R.ok(mallOrderEntity);
    }

    /**
     * 店主取消订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/cancel")
    public R cancelOrder(@LoginUserId Long userId, Long orderId) {
        MallOrderEntity mallOrderEntity = mallOrderService.deleteOrder(userId, orderId);
        return R.ok(mallOrderEntity);
    }
}
